layui.use(['form','laytpl'], function () {
    var form = layui.form,
        layer = layui.layer,
        laytpl = layui.laytpl;
        $ = layui.$;

    //数据回显
    let classInfo;
    //班级下拉选项
    getAjax(false, "/api/stu-class/search/list", '',function (res) {
        if(res.code == 0){
            if(res.data != null){
                classInfo = res.data;
                let htmlStr = '<option value = "">请选择</option>'
                for(let row in res.data){
                    htmlStr += '<option value = "' + res.data[row].id + '">'+res.data[row].className+'（'+res.data[row].grade+'年级'+res.data[row].stuClass+'班'+'）' +'</option>'

                    //班级提示信息
                    if(getJsonRequest("classId") && getJsonRequest("classId").classId == res.data[row].id){
                        dataEngine(laytpl,'class-temp','class-top', res.data[row]);
                    }
                }
                $("#class-view").html(htmlStr);
                form.render('select', 'score-info');
            }
        }else {
            let index = layer.alert(res.msg, {title: '提示', closeBtn: 0}, function () {
                layer.close(index);
                let iframeIndex = parent.layer.getFrameIndex(window.name);
                parent.layer.close(iframeIndex);
            });
            return false;
        }
    });


    //班级下拉框选中
    form.on('select(stu-class)', function(e){
        let element = classInfo.filter(item => item.id == e.value)[0];
        if(!element){
            $(".class-top-body").remove();
            $("#score-form")[0].reset();
        }else {
            //班级提示信息
            dataEngine(laytpl,'class-temp','class-top', element);
            form.render('select', 'score-info');

            //初始化学生下拉框
            getAjax(true, "/api/student/search/list", createJson('classId',element.id),function (res) {
                dataEngine(laytpl,'stu-temp','stu-view', res.data);
                form.render('select', 'score-info');
            });

            //初始课程下拉框
            getAjax(true, "/api/course/search/list", createJson('classId',element.id),function (res) {
                dataEngine(laytpl,'course-temp','course-view', res.data);
                form.render('select', 'score-info');
            });
        }
    });


    //科目下拉框选中
    form.on('select(course)', function(e){
        if(e.value){
            //初始考试下拉框
            getAjax(false, "/api/exam/search/list", createJson('courseId', e.value),function (res) {
                dataEngine(laytpl,'exam-temp','exam-view', res.data);
                form.render('select', 'score-info');
            });
        }
    });


    //监听提交
    form.on('submit(saveBtn)', function (data) {
        postAjax("/api/score/edit", data.field, function (res) {
            if(res.code == 0){
                let index = layer.alert("保存成功", { title: '提示', closeBtn: 0 }, function () {
                    layer.close(index);
                    let iframeIndex = parent.layer.getFrameIndex(window.name);
                    console.log(window.parent.reload())
                    parent.layer.close(iframeIndex);
                });
            }else {
                layer.alert(res.msg, { title: '提示' });
            }
        })
        return false;
    });
});